/*
 * @Description: join方法
 * 
 * @Author: rendc
 * 
 * @Date: 2025-08-20 09:20:31
 * 
 * @LastEditors: rendc
 * 
 * @LastEditTime: 2025-08-20 09:48:50
 */
public class Test01 {

  public static void main(String[] args) {
    // 1. 创建子线程1，每隔10ms输出1-40
    Thread th1 = new Thread() {
      @Override
      public void run() {
        for (int i = 1; i < 41; i++) {
          // System.out.println("th1:" + i);
          try {
            Thread.sleep(10);
          } catch (Exception e) {
            e.printStackTrace();
          }
        }
      }
    };
    // 2. 创建子线程2，每隔10ms输出100-70
    Thread th2 = new Thread(
        new Runnable() {
          @Override
          public void run() {
            for (int i = 100; i >= 70; i--) {
              // th2.getName();
              // Thread.currentThread().getName();
              // System.out.println("th2:" + i);
              try {
                Thread.sleep(10);
              } catch (Exception e) {
                e.printStackTrace();
              }

              // 3. 当线程2输出到80时，执行th1.join()或th1.join(50)
              if (i == 80) {
                System.out.println("join前th1" + th1.getState());
                System.out.println("join前th1" + th1.getState());
                System.out.println("join前th1" + th1.getState());
                System.out.println("join前th1" + th1.getState());
                System.out.println("join前th2" + Thread.currentThread().getState());
                System.out.println("join前th2" + Thread.currentThread().getState());
                System.out.println("join前th2" + Thread.currentThread().getState());
                System.out.println("join前th2" + Thread.currentThread().getState());
                try {
                  // th1.join();
                  th1.join(50);
                  System.out.println("join后th1" + th1.getState());
                  System.out.println("join后th1" + th1.getState());
                  System.out.println("join后th1" + th1.getState());
                  System.out.println("join后th1" + th1.getState());
                  System.out.println("join后th2" + Thread.currentThread().getState());
                  System.out.println("join后th2" + Thread.currentThread().getState());
                  System.out.println("join后th2" + Thread.currentThread().getState());
                  System.out.println("join后th2" + Thread.currentThread().getState());
                } catch (Exception e) {
                  e.printStackTrace();
                }
              }
            }
          };
        });
    // 4. 观察两种join的运行结果
    th1.start();
    th2.start();
  }
}